package com.example.service;

import com.example.enums.BookStatus;
import com.example.mapper.BookInfoMapper;
import com.example.model.BookInfo;
import com.example.model.PageRequest;
import com.example.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2025-04-11
 * Time: 12:26
 */
@Service
public class BookService {


    @Autowired
    private BookInfo bookInfo;
    @Autowired
    private BookInfoMapper bookInfoMapper;


    /**
     * 添加图书
     * @param bookInfo
     * @return
     */

    public Integer insertBook(BookInfo bookInfo) {

        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        //1.获取总记录数
        Integer count = bookInfoMapper.count();

        //2.获取当前页数据
        List<BookInfo> bookInfos = bookInfoMapper.selectBookPage(pageRequest.getOffset(),pageRequest.getPageSize());

        /*//3.处理状态
        //0 删除 1 可借阅 2 不可借阅
        对于可以枚举的信息，可以用枚举类
        for(BookInfo bookInfo:bookInfos){
            if(bookInfo.getStatus() == 2){
                bookInfo.setStatusCN("不可借阅");
            }else if(bookInfo.getStatus() == 1){
                bookInfo.setStatusCN("可借阅");
            }
        }*/
        for(BookInfo bookInfo:bookInfos) {
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfos,count,pageRequest);
    }

    /**
     * 根据Id查询图书信息
     * @param bookId
     * @return
     */
    public BookInfo queryBookById(Integer bookId) {
        //处理状态情况，将状态码转换成文字
        BookInfo bookInfo = bookInfoMapper.queryBookById(bookId);
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    /**
     * 更新图书信息
     * @param bookInfo
     * @return
     */

    public Integer updateBook(BookInfo bookInfo) {

        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {

        return bookInfoMapper.deleteBook(bookId);
    }

    /**
     * 批量删除图书
     * @param bookIds
     * @return
     */

    public Integer batchDeleteBook(List<Integer> bookIds) {
        return bookInfoMapper.batchDeleteBook(bookIds);
    }
}
